package org.seedltd.upms.service.impl.sys;

import java.io.Serializable;

import org.seedltd.core.utils.SqlUtils;
import org.seedltd.upms.mapper.sys.SysCompanyMapper;
import org.seedltd.upms.model.sys.entity.SysCompany;
import org.seedltd.upms.model.sys.form.SysCompanyForm;
import org.seedltd.upms.service.sys.SysCompanyService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;

/**
 * 带缓存的业务实现
 *
 * @author laizuan
 * @version 1.0
 * @since 2021/07/19
 */
@Service
@AllArgsConstructor
@CacheConfig(cacheNames = "SysCompany")
public class SysCompanyServiceImpl implements SysCompanyService {
    private final SysCompanyMapper sysCompanyMapper;

    @Override
    @Cacheable(key = "#id", unless = "#result == null")
    public SysCompany findById(Serializable id) {
        return sysCompanyMapper.selectById(id);
    }

    @Override
    @CacheEvict(key = "#form.companyId")
    public boolean update(SysCompanyForm form) {
        return SqlUtils.u(sysCompanyMapper).updateById(form, form.getCompanyId());
    }

    @Override
    @CacheEvict(key = "#id")
    public boolean delete(Serializable id) {
        return SqlUtils.retBool(sysCompanyMapper.deleteById(id));
    }

    @Override
    @CachePut(key = "#form.companyId", unless = "#result.companyId == null")
    public SysCompany add(SysCompanyForm form) {
        return SqlUtils.u(sysCompanyMapper).save(form, new SysCompany());
    }
}